package sky.wicket.panels;

import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sky.domain.City;
import sky.wicket.AddUpdateCity;
import sky.wicket.CityInfo;
import sky.wicket.HomePage;
import sky.wicket.MyWicketUtils;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 06.10.13
 * Time: 14:56
 * To change this template use File | Settings | File Templates.
 */
public class CityPanel extends Panel {
    private boolean isExpandable = false;
    static final Logger log = LoggerFactory.getLogger(CityPanel.class);

    public CityPanel(String id) {
        super(id);
        throw new RuntimeException("Mistaken constructor");
    }

    public CityPanel(String id, IModel<?> model, boolean isExpandable) {
        super(id, model);
        this.isExpandable = isExpandable;
        City city;
        try {
            city = (City) model.getObject();
        } catch (ClassCastException e) {
            log.error("Cast exception. Expected City.class but found" + model.getObject().getClass().toString() + ".class");
            throw new RuntimeException(e);
        }
        addElementsToPanel(city);
    }

    private void addElementsToPanel(final City city) {
        //info about city
        MyWicketUtils.addBookmarkablePageLinkWithLabelAndParameters(this, "cityNameLink", CityInfo.class,
                new PageParameters().add("cityName", city.getName()), "cityNameLabel", city.getName());
        //edit airport link
        Link editCityLink = new Link("editCityLink") {
            @Override
            public void onClick() {
                setResponsePage(new AddUpdateCity(city.getName()));
            }
        };
        editCityLink.add(new Label("editCityLabel", "[7]"));
        this.add(editCityLink);
        //remove airport link
        Link removeCityLink = new Link("removeCityLink") {
            @Override
            public void onClick() {
                //remove airport
            }
        };
        removeCityLink.add(new Label("removeCityLabel", "[x]"));
        this.add(removeCityLink);
        //add container
        final WebMarkupContainer container = addContainerToList(city);
        this.add(container);
        //make container Expandable if it necessary
        AjaxFallbackLink expandLink = MyWicketUtils.addExpandLink(container, "expandLink", "expandLabel");
        add(expandLink);
        expandLink.setVisible(isExpandable);
    }

    private WebMarkupContainer addContainerToList(City city) {
        final WebMarkupContainer container = new WebMarkupContainer("cityInfoContainer");
        container.setOutputMarkupPlaceholderTag(true);
        container.setVisible(!isExpandable);
        //add labels to container
        container.add(new Label("countryLabel", "Country: "));
        //add links
        //country link
        if (city.getCountry()!=null) {
            MyWicketUtils.addBookmarkablePageLinkWithLabel(container, "countryInfoLink", HomePage.class,
                    "countryInfoLabel", city.getCountry().getName());
        }
        //other elements
        return container;
    }
}
